<template>
    <div>
       <div style="padding:10px 0 20px;">
        <el-button type="primary" size="small" @click="addsecond">添加会场</el-button>
       </div>
       <el-table
    :data="huichang"
    border
    height="400"
    style="width: 100%">
    <el-table-column
      prop="huiname"
      label="会场名称"
      width="240">
    </el-table-column>
    <el-table-column
      prop="starttime"
      label="时间"
      width="120">
    </el-table-column>
    <el-table-column
      prop="times"
      width="240"
      label="创建时间">
    </el-table-column>
    <el-table-column
    prop="state"
    width="200"
      label="状态">
      <template slot-scope="scope">
        <p>{{ scope.row.state=='1'?'开启':'关闭' }}</p>
      </template>
    </el-table-column>

    <el-table-column
    prop="spnums"
    width="200"
      label="商品数量">
    </el-table-column>
    <el-table-column
      label="操作"
      >
      <template slot-scope="scope">
        <el-button @click="handleClick(scope.row,0)" type="text" size="small">修改</el-button>
        <el-button @click="handleClick(scope.row,1)" type="text" size="small">删除</el-button>
        <el-button @click="handleClick(scope.row,2)" type="text" size="small">查看信息</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div style="width: 100%; display: flex; margin: 10px 0; justify-content: flex-end;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[3,6,10,100]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="nums">
    </el-pagination>
  </div>
    </div>
</template>

<script>
import {getmsjhuichang,delmsjhuichang,getmsjhuichangshops,msjsethuichang} from '../../../utils/api'
export default {
    name: 'PhaemacySecondHuichang',

    data() {
        return {
            huichang:[],
            tableData:[],
            huishops:[],
            nums:0,
            pagesize:3,
            currentPage4: 1
        };
    },
    created(){
      this.$bus.$on('getshop',this.getshop)
      this.getshops()
    },
    mounted() {
        this.gethuichang()
    },

    methods: {
      // 获取所有会场
      gethuichang(){
        getmsjhuichang().then(res=>{
          let data=res.data.data.map(v=>{
              v.spnums=this.huishops.filter(item=>item.sshuhuichang==v.huiname).length
              this.sethui(v)
              return v
          })
            this.tableData=data
            this.nums=data.length
            this.fenye(data)
        })
      },
      sethui(v){
        msjsethuichang(v)
      },
      // 
      getshop(){
        console.log(111);
        this.getshops()
        this.gethuichang()
      },
      getshops(){
        getmsjhuichangshops().then(res=>{
          this.huishops=res.data.data
        })
      },
      // 删除 跳转修改 查看信息
      handleClick(v,n){
        if(n==1){
          delmsjhuichang(v.id).then(res=>{
          this.$message({
            message: res.data.msg,
            type: 'success'
          })
          this.gethuichang()
        })
        }else if(n==0){ 
          let obj={
            data:v,
            add:0
          }
          this.$router.push({
            name:'addseconds',
            params:obj
          })
          sessionStorage.setItem('huichang',JSON.stringify(obj))
        }else{
          this.$emit('onbreadcrumb',{name:'商品管理',curr:1})
          this.$bus.$emit('getcurr',{curr:1})
        }
        
      },
      // 分页
      fenye(v){
        let arr=JSON.parse(JSON.stringify(v))
        let start=(this.currentPage4-1)*this.pagesize
        this.huichang=arr.splice(start,this.pagesize)
        if(this.huichang.length==0){
          this.currentPage4=(this.currentPage4-1)>1?(this.currentPage4-1):1
          this.gethuichang()
        }
        
      },
      handleSizeChange(val) {
        this.pagesize=val
        this.fenye(this.tableData)
      },
      handleCurrentChange(val) {
        this.currentPage4=val
        this.fenye(this.tableData)
      },
      // 跳转添加 
      addsecond(){
        this.$router.push({
          name:'addseconds',
          params:{
            add:1,
          }
        })
        sessionStorage.setItem('huichang',JSON.stringify({add:1}))
      }
    },
};
</script>

<style  scoped>

</style>